package com.gesee.zyspps.activity;

import java.util.ArrayList;
import java.util.List;

import com.gesee.zys.R;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.db.CmsLine;
import com.gesee.zyspps.db.CmsLineDao.Properties;
import com.gesee.zyspps.utils.LogUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 
 * @ClassName: DrawPointActivity
 * @Description: TODO(修改管线属性)
 * @author fujianxiong
 * @date 2015年8月15日 上午10:25:43
 *
 */
public class UpdateLineActivity extends BaseActivity implements OnClickListener
{
    private Button btnAccept;// 确定
    private Button btnCancel;// 取消
    private TextView title;//标题
    private EditText et_qddh; // 起点点号
    private EditText et_zddh;// 终点点号
    private EditText et_qdms;// 起点埋深
    private EditText et_zdms; // 终点埋深
    private EditText et_qdgp;// 起点管偏
    private EditText et_zdgp;// 终点管偏
    private EditText et_cz;// 材质
    private EditText et_msfs;// 埋设方式
    private EditText et_gj;// 管径
    private EditText et_dyyl;// 电压压力
    private EditText et_lx;// 流向
    private EditText et_dlmc;// 道路名称
    private EditText et_bz;// 备注

    private Spinner sp_cz;// 材质
    private Spinner sp_msfs;// 埋设方式
    private Spinner sp_dyyl;// 电压压力
    private Spinner sp_lx;// 流向
    
    private String[] czItems;
    private String[] msfsItems;
    private String[] dyylItems;
    private String[] lxItems;
    
    private LinearLayout ll_dlts;// 电缆条数
    private LinearLayout ll_yyks;// 已用孔数
    private LinearLayout ll_zks;// 总孔数
    
    private EditText et_dlts;
    private EditText et_yyks;
    private EditText et_zks;
    
    private ArrayAdapter<String> czAdapter;
    private ArrayAdapter<String> msfsAdapter;
    private ArrayAdapter<String> dyylAdapter;
    private ArrayAdapter<String> lxAdapter;
    
    private boolean flag1 = false;
    private boolean flag2 = false;
    private boolean flag3 = false;
    private boolean flag4 = false;
    
    private String startPoint;
    private String endPoint;
    private long lineId;
    private List<CmsLine> lineList = new ArrayList<CmsLine>();

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_line);
        findView();
        initView();
    }

    private void findView()
    {
        btnAccept = (Button) findViewById(R.id.btnAccept);
        btnCancel = (Button) findViewById(R.id.btnCancel);
        title = (TextView) findViewById(R.id.title);
        et_qddh = (EditText) findViewById(R.id.et_qddh);
        et_zddh = (EditText) findViewById(R.id.et_zddh);
        et_qdms = (EditText) findViewById(R.id.et_qdms);
        et_zdms = (EditText) findViewById(R.id.et_zdms);
        et_qdgp = (EditText) findViewById(R.id.et_qdgp);
        et_zdgp = (EditText) findViewById(R.id.et_zdgp);
        et_cz = (EditText) findViewById(R.id.et_cz);
        et_msfs = (EditText) findViewById(R.id.et_msfs);
        et_gj = (EditText) findViewById(R.id.et_gj);
        et_dyyl = (EditText) findViewById(R.id.et_dyyl);
        et_lx = (EditText) findViewById(R.id.et_lx);
        et_dlmc = (EditText) findViewById(R.id.et_dlmc);
        et_bz = (EditText) findViewById(R.id.et_bz);
        
        ll_dlts = (LinearLayout) findViewById(R.id.ll_dlts);
        ll_yyks = (LinearLayout) findViewById(R.id.ll_yyks);
        ll_zks = (LinearLayout) findViewById(R.id.ll_zks);
        et_dlts = (EditText) findViewById(R.id.et_dlts);
        et_yyks = (EditText) findViewById(R.id.et_yyks);
        et_zks = (EditText) findViewById(R.id.et_zks);

        sp_cz = (Spinner) findViewById(R.id.sp_cz);
        sp_msfs = (Spinner) findViewById(R.id.sp_msfs);
        sp_dyyl = (Spinner) findViewById(R.id.sp_dyyl);
        sp_lx = (Spinner) findViewById(R.id.sp_lx);
        
        title.setText("修改管线");
    }

    private void initView()
    {
        btnAccept.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        intentView();
        setSpinner();
    }

    private void intentView()
    {
        Intent intent = getIntent();
        startPoint = intent.getStringExtra("startPoint");
        endPoint = intent.getStringExtra("endPoint");
        
        lineList = ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsLineDao()
                .queryBuilder().where(Properties.S_Point.eq(startPoint),Properties.E_Point.eq(endPoint)).build().list();

        for (int i = 0; i < lineList.size(); i++)
        {
            lineId = lineList.get(i).getId();
            et_qddh.setText(lineList.get(i).getS_Point());
            et_zddh.setText(lineList.get(i).getE_Point());
            et_qdms.setText(lineList.get(i).getS_Deep());
            et_zdms.setText(lineList.get(i).getE_Deep());
            et_qdgp.setText(lineList.get(i).getS_D());
            et_zdgp.setText(lineList.get(i).getE_D());
            et_cz.setText(lineList.get(i).getMaterial());
            et_msfs.setText(lineList.get(i).getDType());
            et_gj.setText(lineList.get(i).getDSize());
            et_dyyl.setText(lineList.get(i).getVoltage()); //电压压力
            et_lx.setText(lineList.get(i).getDirection());
            et_dlmc.setText(lineList.get(i).getRoad());
            et_bz.setText(lineList.get(i).getRemark());
             et_dlts.setText(lineList.get(i).getCableLines());
            et_yyks.setText(lineList.get(i).getUseHole());
            et_zks.setText(lineList.get(i).getTotalHole());
        }
        
        setIntentData(startPoint, endPoint);
    }

    private void setIntentData(String startPoint, String endPoint)
    {
        et_qddh.setText(startPoint);
        et_zddh.setText(endPoint);
    }
    
    private void setSpinner()
    {
        czItems = getResources().getStringArray(R.array.line_cz);
        czAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, czItems);
        czAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_cz.setAdapter(czAdapter);
        
        for(int i = 0;i<czItems.length;i++){
            if(czItems[i].toString().equals(lineList.get(0).getMaterial())){
                sp_cz.setSelection(i);
            }
        }
        
        msfsItems = getResources().getStringArray(R.array.line_msfs);
        msfsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, msfsItems);
        msfsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_msfs.setAdapter(msfsAdapter);
        
        for(int i = 0;i<msfsItems.length;i++){
            if(msfsItems[i].toString().equals(lineList.get(0).getDType())){
                sp_msfs.setSelection(i);
            }
        }
        
        dyylItems = getResources().getStringArray(R.array.line_dyyl);
        dyylAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dyylItems);
        dyylAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_dyyl.setAdapter(dyylAdapter);
        
        for(int i = 0;i<dyylItems.length;i++){
            if(dyylItems[i].toString().equals(lineList.get(0).getVoltage())){
                sp_dyyl.setSelection(i);
            }
        }
        
        lxItems = getResources().getStringArray(R.array.line_lx);
        lxAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lxItems);
        lxAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_lx.setAdapter(lxAdapter);
        
        for(int i = 0;i<lxItems.length;i++){
            if(lxItems[i].toString().equals(lineList.get(0).getDirection())){
                sp_lx.setSelection(i);
            }
        }
        
        sp_cz.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
               /* if (position == 0)
                {
                    LogUtil.d("默认不选中");
                }
                else
                {*/
                if (parent.getItemAtPosition(position).toString().equals(lineList.get(0).getMaterial()))
                {
                    et_cz.setText(parent.getItemAtPosition(position).toString());
                }
                if(flag1){
                    et_cz.setText(parent.getItemAtPosition(position).toString());
                }
                flag1 = true;
                    //et_cz.setText(parent.getItemAtPosition(position).toString());
                //}

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });
        
        sp_msfs.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
               /* if (position == 0)
                {
                    LogUtil.d("默认不选中");
                }
                else
                {*/
                if (parent.getItemAtPosition(position).toString().equals(lineList.get(0).getDType()))
                {
                    et_msfs.setText(parent.getItemAtPosition(position).toString());
                }
                if(flag2){
                    et_msfs.setText(parent.getItemAtPosition(position).toString());
                }
                flag2 = true;
                    //et_msfs.setText(parent.getItemAtPosition(position).toString());
               // }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });
        
        sp_dyyl.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
                /*if (position == 0)
                {
                    LogUtil.d("默认不选中");
                }
                else
                {*/
                if (parent.getItemAtPosition(position).toString().equals(lineList.get(0).getVoltage()))
                {
                    et_dyyl.setText(parent.getItemAtPosition(position).toString());
                }
                if(flag3){
                    et_dyyl.setText(parent.getItemAtPosition(position).toString());
                }
                flag3 = true;
                   // et_dyyl.setText(parent.getItemAtPosition(position).toString());
                //}

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });
        
        sp_lx.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                // TODO Auto-generated method stub
                /*if (position == 0)
                {
                    LogUtil.d("默认不选中");
                }
                else
                {*/
                if (parent.getItemAtPosition(position).toString().equals(lineList.get(0).getDirection()))
                {
                    et_lx.setText(parent.getItemAtPosition(position).toString());
                }
                if(flag4){
                    et_lx.setText(parent.getItemAtPosition(position).toString());
                }
                flag4 = true;
                    //et_lx.setText(parent.getItemAtPosition(position).toString());
                //}

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }
        });
    }

    @Override
    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
        case R.id.btnAccept:
            String qddh = et_qddh.getText().toString();
            String zddh = et_zddh.getText().toString();
            String qdms = et_qdms.getText().toString();
            String zdms = et_zdms.getText().toString();
            String qdgp = et_qdgp.getText().toString();
            String zdgp = et_zdgp.getText().toString();
            String cz = et_cz.getText().toString();
            String msfs = et_msfs.getText().toString();
            String gj = et_gj.getText().toString();
            String dyyl = et_dyyl.getText().toString();
            String lx = et_lx.getText().toString();
            String dlmc = et_dlmc.getText().toString();
            String bz = et_bz.getText().toString();
            String dlts = et_dlts.getText().toString();
            String yyks = et_yyks.getText().toString();
            String zks = et_zks.getText().toString();
            
            CmsLine line = new CmsLine();
            line.setId(lineId);
            line.setS_Point(qddh);
            line.setE_Point(zddh);
            line.setS_Deep(qdms);
            line.setE_Deep(zdms);
            line.setS_D(qdgp);
            line.setE_D(zdgp);
            line.setMaterial(cz);
            line.setDType(msfs);
            line.setDSize(gj);
            line.setVoltage(dyyl); //电压压力
            line.setDirection(lx);
            line.setRoad(dlmc);
            line.setRemark(bz);
            line.setCableLines(dlts);
            line.setUseHole(yyks);
            line.setTotalHole(zks);
            
            ZSApplication.getInstance().setConfig("startPoint", qddh);
            ZSApplication.getInstance().setConfig("endPoint", zddh);
            ZSApplication.getDaoSession(ZSApplication.getInstance().getConfig("newDBName")).getCmsLineDao().updateInTx(line);
            ZSApplication.getInstance().finishActivity();
            break;
        case R.id.btnCancel:
            ZSApplication.getInstance().finishActivity();
            break;

        default:
            break;
        }
    }
}
